package com.danco.training.zaneuskaya.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.UserDao;
import com.danco.training.zaneuskaya.service.SearchService;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("searchService")
@Transactional
public class SearchServiceImpl implements SearchService {

	@Autowired
	private UserDao userDao;

	private static final Logger LOGGER = Logger
			.getLogger(SearchServiceImpl.class);

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public List<User> findUserNamesByName(String name) {
		List<User> str = new ArrayList<User>();
		try {
			List<User> users = userDao.readAll();
			for (User user : users) {
				if (user.getFirstName().startsWith(name)
						|| user.getLastName().startsWith(name)) {
					str.add(user);
				}
			}
			return str;
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return null;
		}

	}

}
